from flask import request

from apps.settingApi.switchCase.caseAallFileds import allColumns
from apps.settingApi.switchCase.caseFilter import CASEFILTER
from apps.settingApi.switchCase.caseParameter import caseParameter
from apps.settingApi.switchCase.caseRequired import caseRequiredExist
from apps.settingApi.switchCase.caseType import caseColumnsType

# 获取form跟json的请求参数
def getArgsData(args=None):
    if args == None:
        if request.is_json == True:
            args = request.get_json()
        else:
            args = request.form
        if len(args) == 0:
            return None,'参数不能为空'
    argsObj = {}
    for itm in args.keys():
        argsObj[itm] = args[itm]
    return argsObj,None

# 序列化form跟json的请求参数
def serializeParData(model,argsData=None):
    """
    序列化返回参数
    modelObj:模型
    argsData参数
    """
    args,err = getArgsData(argsData)
    if err!=None:
        return None, err
    columnsList = allColumns(model)
    # 校验必填字段是否传递（如果是put修改的话则不校验必填字段）
    _, err = caseRequiredExist(args, columnsList,request.method.lower())
    if err != None:
        return None,err
    # 校验数据类型
    _, err = caseColumnsType(args, columnsList)
    if err != None:
        return None,err

    # 拼接返回的数据
    parameterDict = caseParameter(args, columnsList)
    return parameterDict,None

# 获取get请求参数
def getParmData(argsData=None):
    # 获取所有参数get参数
    if argsData == None:
        getObj = request.args
        args = {}
        getObjKeys = getObj.keys()
        for itm in getObjKeys:
            args[itm] = getObj[itm]
        if "pageSize" not in getObj.keys():
            args['pageSize'] = 10
        else:
            args['pageSize'] = int(args['pageSize'])
        if "page" not in getObj.keys():
            args['page'] = 1
        else:
            args['page'] = int(args['page'])
    else:
        args = argsData
        if "pageSize" not in args.keys():
            args['pageSize'] = 10
        else:
            args['pageSize'] = int(args['pageSize'])
        if "page" not in args.keys():
            args['page'] = 1
        else:
            args['page'] = int(args['page'])
    return args

# 获取get请求参数并自动拼接查询列表
def serializeGetFilter(model,argsData=None):
    """

    :param model: 模型
    :param argsData: 参数
    :return:
    """
    filterList = []
    args = getParmData(argsData)
    print(999,args)
    for itm1 in args:
        # 分割参数用于判断查询类型（等于、大于等于、模糊查询等）
        itmSplit = itm1.split('__', 1)
        filterTpye = ''
        filterFiled = itmSplit[0]
        if len(itmSplit) > 1:
            filterTpye = itmSplit[1]
        if hasattr(model, filterFiled):
            # 获取查询方式
            filter_z = CASEFILTER.get(filterTpye, CASEFILTER['default'])(model, filterFiled, args[itm1])
            filterList.append(filter_z)
    return filterList,args

def pageResultData(pageData):
    """
    拼接分页返回的数据
    :param pageData: 分页数据对象
    :return:
    """
    objData = {}
    objData["total"] = pageData.total  # 总记录数
    objData["has_next"] = pageData.has_next  # 是否有下一页
    objData["has_prev"] = pageData.has_prev  # 是否有上一页
    objData["page"] = pageData.page  # 当前页
    objData["pages"] = pageData.pages  # 总页数
    return objData